/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.report;

import java.util.HashMap;

import com.bluestone.IAction;
import com.bluestone.util.Util;

/**
 * ActionTimeHandler records the time consume when IAction is invoked.
 * @author <a href="mailto:ming7655@gmail.com">Aaron</a>
 */
public class ActionTimeHandler extends AbstractActionHandler {
	public ActionTimeHandler(String type) {
		super(type);
	}

	private long begin=0;
	private long end=0;
	private long timeConsume = 0;
	private boolean isNeedReset = false;
	private HashMap map = new HashMap();
	
	/* (non-Javadoc)
	 * @see com.bluestone.run.IActionHandler#process(IAction action)
	 */
	public void process(IAction action){
		super.process(action);
		if(isNeedReset){
			end = System.currentTimeMillis();
			this.timeConsume = end-begin;
			Util.warn("The action of the "+this.action.getName()+" 's timeConsume is :"+this.timeConsume);
			map.put(AbstractExpAttributeHandler.EXPATTR_TIMECONSUME, String.valueOf(this.timeConsume));
			report();
			this.reset();
		}else{
			begin = System.currentTimeMillis();
			this.isNeedReset = true;
		}
	}

	public String getType() {
		return IActionHandler.PREP_POST;
	}
	
	private void reset(){
		this.begin = 0;
		this.end = 0;
		this.timeConsume = 0;
		this.isNeedReset=false;
	}

	/**
	 * Set the value for the expand attribute of "TimeConsume".
	 */
	public HashMap handlerExpAttributes(Object source,Object value){		
		return map;
	}

}
